package com.vermicelli.controller;

import com.usian.articles.Articles;
import com.usian.dz.Dz;
import com.usian.user.User;
import com.usian.vo.ResultVo;
import com.vermicelli.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/app/column/collection")
public class CollectionCtroller {
    @Autowired
    private CollectionService collectionService;

    @RequestMapping("/{id}")
    public ResultVo collection(@PathVariable Integer id) {
        if (id == null){
            return ResultVo.error("请传入用户id");
        }
        List<Articles> articlesList = collectionService.getCollection(id);
        if (articlesList!=null){
            return ResultVo.ok(articlesList);
        }else {
            return ResultVo.error("没有收藏记录");
        }
    }

    /**
     * 处理收藏删除请求
     *
     * @param id 待删除的收藏ID，不能为空
     * @return 返回操作结果，若ID为空则返回错误提示
     */
    @RequestMapping("/del/{id}/{userid}")
    public ResultVo del(@PathVariable Integer id,@PathVariable Integer userid) {
        // 检查传入的收藏ID是否为空，为空则返回错误信息
        if (id == null){
            return ResultVo.error("请传入收藏id");
        }
        // 调用服务层删除方法，并返回删除成功的结果
        return ResultVo.ok(collectionService.del(id,userid));
    }
    @GetMapping("/comments/{id}/{status}")
    public ResultVo comments(@PathVariable Integer id,@PathVariable Integer status){
         List<Dz> dzList = collectionService.comments(id,status);
        for (Dz dz : dzList) {
            User user = collectionService.selectUserOne(dz.getUserId());
            if (user!=null){
                dz.setName(user.getName());
                dz.setImage(user.getImage());
            }
        }
        return ResultVo.ok(dzList);
    }
    @PostMapping("/addComment")
    public ResultVo addComment(@RequestBody Dz dz){
        return ResultVo.ok(collectionService.addComment(dz));
    }
}
